package com.wans.guli.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.wans.guli.product.vo.CategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wans.guli.product.entity.CategoryEntity;
import com.wans.guli.product.service.CategoryService;
import com.wans.guli.common.utils.PageUtils;
import com.wans.guli.common.utils.R;


/**
 * 商品三级分类
 *
 * @author wans
 * @email wansconst@163.com
 * @date 2025-08-01 15:14:35
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;



    /**
     * 树形列表
     */
    @RequestMapping("/list/tree")
    //@RequiresPermissions("product:category:list")
    public R listTree() {
        List<CategoryVO> listTree = categoryService.listTree();

        return R.ok().put("data", listTree);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R saveCategory(@RequestBody CategoryEntity category) {
        categoryService.saveCategory(category);

        return R.ok();
    }

    /**
     * 保存
     */
    //@RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateCategory(category);

        return R.ok();
    }

    /**
     * 修改排序
     */
    @PutMapping("/update/sort")
    public R updateSort(@RequestBody CategoryEntity[] category) {
        Arrays.stream(category).forEach(item -> {
            item.setName(null);
            item.setIcon(null);
            item.setProductUnit(null);
            item.setProductCount(null);
            item.setShowStatus(null);
        });
        categoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R deleteCategory(@RequestBody Long[] catIds) {
        categoryService.removeCategoryByIds(Arrays.asList(catIds));

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds) {
        categoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
